<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 400px;
            margin: 50px auto;
            padding: 20px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="email"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .login-link {
            text-align: center;
            margin-top: 15px;
        }
        .login-link a {
            color: #007bff;
            text-decoration: none;
        }
        .login-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>用户注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" >
        </div>
        <div class="form-group">
            <label for="email">电子邮箱</label>
            <input type="email" id="email" name="email">
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" >
        </div>
        <div class="form-group">
            <label for="confirmPassword">确认密码</label>
            <input type="password" id="confirmPassword" name="confirmPassword" >
        </div>


        <button type="submit" onclick="enroll()" >注册</button>
    </form>
    <div class="login-link">
        已有账号？<a href="blog_login.html">登录</a>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    function enroll(){

        const username = $('#username').val().trim();
        const email = $('#email').val().trim();
        const password = $('#password').val();
        const confirmPassword = $('#confirmPassword').val();

    // 验证函数示例
        if (!username) {
            alert("用户名不能把为空")
            return ;
        }
        if (!email) {
            alert("电子邮件不能为空 ")
            return ;
        }
        if (!password) {
            alert("密码不能为空 ")
            return ;
        }
        if (!/^[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9]+$/.test(email)) {
            alert("电子邮件格式不正确 ")
            return ;
        }

        if (password !== confirmPassword) {
            alert('两次输入的密码不一致！');
            return;
        }




        // 发送AJAX请求
        $.ajax({
            type: "POST",
            url: "user/enroll",
            data: {
                userName: username,
                password: password,
                email: email,
            },
            success: function(result) {
                if (result.code == "SUCCESS" && result.data =="注册成功") {
                    console.log("111")
                    alert(result.data);
                    location.href = "blog_login.html";
                }else {
                    console.log(result)
                    alert(result.data);
                }
            },
            error:function (error){
                console.log(error)
            }
        });
    }
</script>
</body>
</html>